package cn.edu.scau.cmi.houzhicong.hibernate.dao;

import cn.edu.scau.cmi.houzhicong.hibernate.domain.Student;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;


public class StudentDao {

    private static String configureFile = "/cn/edu/scau/cmi/houzhicong/hibernate/hibernate.cfg.xml";
    private static SessionFactory factory = new Configuration().configure(configureFile).buildSessionFactory();


    public boolean save(String id, String name){
        Session session = factory.openSession();
        Transaction tx = null;  //事务
        String studentID = null;
        try{
            tx = session.beginTransaction();
            Student student = new Student(id, name);
            studentID = (String) session.save(student);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
            studentID = null;
        }finally {
            session.close();
        }
        return studentID!=null;
    }

    public void deleteById(String id){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Student student = (Student) session.get(Student.class, id);
            session.delete(student);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }



    public Student findById(String id){
        Session session = factory.openSession();
        Transaction tx = null;
        Student student = null;
        try{
            tx = session.beginTransaction();
            student = (Student) session.get(Student.class, id);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            student = null;
            e.printStackTrace();
        }finally {
            session.close();
        }
        return student;
    }



    public void update(Student student){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            session.update(student);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }


    public void chooseTutor(String id, String tutor_id){
        Session session = factory.openSession();
        Transaction tx = null;
        try{
            tx = session.beginTransaction();
            Student student = (Student) session.get(Student.class, id);
            student.getTutor().setId(tutor_id);
            session.update(student);
            tx.commit();
        }catch (Exception e) {
            if (tx!=null) tx.rollback();
            e.printStackTrace();
        }finally {
            session.close();
        }
    }

}
